package io.kiki.stack.http.feign.codec;

import io.kiki.stack.http.feign.Response;
import io.kiki.stack.http.feign.Util;

import java.io.IOException;
import java.lang.reflect.Type;

import static java.lang.String.format;

public class StringDecoder implements Decoder {

    @Override
    public Object decode(Response response, Type type) throws IOException {
        Response.Body body = response.body();
        if (response.status() == 404 || response.status() == 204 || body == null) {
            return null;
        }
        if (String.class.equals(type)) {
            return Util.toString(body.asReader(Util.UTF_8));
        }
        throw new DecodeException(response.status(), format("%s is not a type supported by this decoder.", type), response.request());
    }
}
